<template>
  <div>
    <!-- 背景图 -->
    <van-image
      width="10rem"
      height="5.1rem"
      src="http://liufusong.top:8080/img/profile/bg.png"
    />
    <!-- 登录盒子 -->
    <div class="login" v-if="!show">
      <div class="img">
        <img
          class="img2"
          src="http://liufusong.top:8080/img/profile/avatar.png"
          alt=""
        />
      </div>
      <div class="text">游客</div>
      <div class="loginBut">
        <van-button
          type="primary"
          color=""
          size="small"
          class="btn"
          @click="$router.push('/login')"
          >点击登录</van-button
        >
      </div>
    </div>
       <div class="login" v-else>
      <div class="img">
        <img
          class="img2"
          :src="imgurl+tokens.avatar"
          alt=""
        />
      </div>
      <div class="text">{{tokens.nickname}}</div>
      <div class="loginBut">
        <van-button
          type="primary"
          color="#21b97a"
          size="small"
          class="btn"
          @click="backloginFn"
          >退出登录</van-button
        >
      </div>
    </div>
    <!-- 个人中心 -->
    <div class="my">
      <van-grid :column-num="3">
        <van-grid-item icon="star-o" text="我的收藏" @click="favoriteFn"/>
        <van-grid-item icon="wap-home-o" text="我的出租" @click="torentFn"/>
        <van-grid-item icon="clock-o" text="看房记录" />
        <van-grid-item icon="records" text="成为房主" />
        <van-grid-item icon="manager-o" text="个人资料" />
        <van-grid-item icon="service-o" text="联系我们" />
      </van-grid>
    </div>
    <!-- 图片 -->
    <div class="imgs">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="" />
    </div>
  </div>
</template>

<script>
import { userLoginmessage } from '@/api'
import { Dialog } from 'vant'
export default {
  data () {
    return {
      show: !!localStorage.getItem('token'),
      tokens: {},
      imgurl: 'http://liufusong.top:8080'
    }
  },
  //
  created () {
    if (localStorage.getItem('token')) this.houseUser()
  },
  methods: {
    //  获取登录用户资料
    async houseUser () {
      const res = await userLoginmessage({
        authorization: localStorage.getItem('token')
      })
      console.log(res)
      this.tokens = res.data.body
      console.log(localStorage.getItem('token'))
    },
    // 点击退出登录
    backloginFn () {
      Dialog.confirm({
        title: '温馨提示',
        message: '确定要退出登陆吗'
      }).then(() => {
        // 清空本地token
        this.$store.commit('setToken', '')
        this.$router.go(0)
      })
    },
    // 查看收藏列表
    favoriteFn () {
      this.$router.push({
        path: '/myFavorite'
      })
    },
    // 我的出租 /MyTorent
    torentFn () {
      this.$router.push({
        path: '/MyTorent'
      })
    }
  }
}
</script>

<style>
.login {
  position: absolute;
  background: #fff;
  width: 74%;
  height: 150px;
  bottom: 10.45rem;
  left: 50%;
  transform: translateX(-50%);
  box-shadow: 0 0 10px 3px #ddd;
  margin: 50px auto 0;
  padding: 0 20px;
  text-align: center;
  font-size: 13px;
}
.img {
  position: relative;
  transform: translateY(-50%);
  border-radius: 50%;
  width: 54px;
  height: 54px;
  border: 5px solid #f5f5f5;
  display: inline-block;
  box-shadow: 0 2px 2px #bdbdbd;
}
.img2 {
  width: 100%;
  border-radius: 50%;
}
.text {
  margin-top: -17px;
  margin-bottom: 10px;
}
.loginBut {
  color: #999;
  font-size: 12px;
  margin-top: 15px;
}
.btn {
  width: 67px;
  height: 27px;
  border-radius: 8%;
}
/* 个人中心盒子 */
.my {
  margin-top: 100px;
}
.imgs {
  width: 355px;
  margin-left: 10px;
  margin-top: 10px;
}
img {
  width: 100%;
}
</style>
